package com.qst.crop.demo.controller;

import com.qst.crop.demo.common.Result;
//import com.qst.crop.demo.common.UserSessionUtils;
import com.qst.crop.demo.entity.OrderTable;
import com.qst.crop.demo.entity.Product;
import com.qst.crop.demo.entity.ShoppingCart;
import com.qst.crop.demo.entity.User;
import com.qst.crop.demo.service.CartService;
import com.qst.crop.demo.service.OrderService;
import com.qst.crop.demo.service.ProductService;
import com.qst.crop.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/order")
@CrossOrigin(origins = "*")
public class OrderController {

    @Autowired
    OrderService orderService;
    @Autowired
    ProductService productService;
    @Autowired
    UserService userService;


    /**
     *  添加商品
     * @param sellerEmail
     * @param customerEmail
     * @param productId
     * @return
     */
    @RequestMapping("/add")
    public Result add(OrderTable order){
        System.out.println("添加商品");

        if( order == null || order.getProductId() <= 0 || !StringUtils.hasLength(order.getSellerEmail()) || !StringUtils.hasLength(order.getCustomerEmail())){
            return Result.fail(-1, "非法参数");
        }

        User seller = userService.getUserByEmail(order.getSellerEmail());
        User customer = userService.getUserByEmail(order.getCustomerEmail());
        if(seller == null || customer == null){
            return Result.fail(-2, "不存在该用户");
        }

        Product product = productService.getProById(order.getProductId());
        if(product == null){
            return Result.fail(-3, "商品不存在");
        }

        // 如果 用户已经添加了该商品， 那就增加数量
        // 否则 直接增加数据
        if(orderService.updateQua(order.getProductId(), order.getSellerEmail(), order.getCustomerEmail()) == 0){
            order.setOrderDate(new Timestamp(new Date().getTime()));
            order.setQuantity(1);
            order.setTotalPrice((productService.getProById(order.getProductId())).getProductPrice());
            order.setCustomerName(customer.getNickname());

            orderService.add(order);
        }else {
            double price = product.getProductPrice();
            long quantity = orderService.getQua(order.getProductId(), order.getSellerEmail(), order.getCustomerEmail());
            double totalPrice = price * quantity;

            orderService.updatePri(totalPrice, order.getProductId(), order.getSellerEmail(), order.getCustomerEmail());
        }

        OrderTable orderTable = orderService.getOrderByPro(order.getProductId(), order.getSellerEmail(), order.getCustomerEmail());

        return Result.success(200, orderTable, "增加成功");
    }

    /**
     *  删除商品
     * @param sellerEmail
     * @param customerEmail(email)
     * @param productId
     * @return
     */
    @RequestMapping("/del")
    public Result del(String customerEmail, String sellerEmail, Long productId){
        if(productId <= 0 || !StringUtils.hasLength(customerEmail) || !StringUtils.hasLength(sellerEmail)){
            return Result.fail(-1, "非法参数");
        }

        User user = userService.getUserByEmail(customerEmail);
        if(user == null){
            return Result.fail(-2, "不存在该用户");
        }

        Product product = productService.getProById(productId);
        if(product == null){
            return Result.fail(-3, "不存在该商品");
        }

        // 如果用户已经添加了该商品， 那就减数量
        // 否则减商品
        if(orderService.updateQua2(productId, sellerEmail, customerEmail) == 1){
            if(orderService.getQua(productId, sellerEmail, customerEmail) == 0){
                orderService.del(productId, sellerEmail, customerEmail);
            }

            return Result.success(200, "删除成功");
        }

        return Result.success(-4, "该用户没加入该商品至购物车");
    }


    /**
     *  获取全部
     * @param email
     * @return
     */
    @RequestMapping("/getAll")
    public Result getAll(String email){
        User user = userService.getUserByEmail(email);

        if(user == null || !StringUtils.hasLength(user.getEmail())){
            return Result.fail(-1, "用户不存在");
        }

        List<OrderTable> orderTables = orderService.getAll(email);

        return Result.success(200, orderTables);
    }
}
